package org.bdware.doip.core.doipMessage;

public class MessageHeader {

    public boolean IsRequest;

    private int flag;
    public int parameterLength;
    public int bodyLength;

    public HeaderParameter parameters;

    private transient boolean isRequest = false;
    private transient boolean isCertified = false;
    private transient boolean isEncrypted = false;

    public MessageHeader(String id, String opCode){
        flag = 0;
        parameters = new HeaderParameter(id,opCode);
        parameterLength = parameters.length();
    }

    public MessageHeader(String id, String opCode, int f){
        this.setFlag(f);
        parameters = new HeaderParameter(id,opCode);
        parameterLength = parameters.length();
    }

    private boolean isRequest0(){
        return this.flag < 0;
    }

    private boolean isCertified0(){
        return (flag << 1 ) < 0 ;
    }

    public boolean isRequest(){return isRequest;}
    public boolean isCertified(){return isCertified;}
    public boolean isEncrypted(){return isEncrypted;}

    public void setIsRequest(boolean isRequest){
        this.isRequest = isRequest;
        updateFlag();
    }

    public void setIsEncrypted(boolean isEncrypted){
        this.isEncrypted = isEncrypted;
    }

    public void setIsCertified(boolean isCertified){
        this.isCertified = isCertified;
        updateFlag();
    }

    public void setFlag(int f){
        this.flag = f;
        if(isRequest0()) isRequest = true;
        if(isCertified0()) isCertified = true;
    }
    public int getFlag(){
        return this.flag;
    }

    private void updateFlag(){
        this.flag = 0;
        if(isRequest) this.flag = (this.flag | 0x80000000);
        if(isCertified) this.flag = (this.flag | 0x4000000);
    }

}
